package com.jeesite.modules.dataScreen.service;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ArrayUtil;
import com.jeesite.modules.dataScreen.web.vo.AirportInformationVo;
import com.jeesite.modules.dataScreen.web.vo.IndustryIndexVo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.*;

import static com.jeesite.modules.dataScreen.web.vo.IndustryIndexFun.fun;

@Service
public class AirportInformationService {

    private static final String[] JC_PARAMS = new String[]{"江北机场航站楼停机位数量",
            "黔江机场航站楼停机位数量",
            "万州机场航站楼停机位数量",
            "巫山机场航站楼停机位数量",
            "仙女山机场航站楼停机位数量",
            "全市运输机场航站楼停机位总个数"};

    private static final String[] PD_PARAMS = new String[]{"江北机场跑道条数",
            "黔江机场跑道条数",
            "万州机场跑道条数",
            "巫山机场跑道条数",
            "仙女山机场跑道条数",
            "全市运输机场跑道总条数"};
    private static final String[] LKTTNL_PARAMS = new String[]{"江北机场旅客吞吐能力",
            "黔江机场旅客吞吐能力",
            "万州机场旅客吞吐能力",
            "巫山机场旅客吞吐能力",
            "仙女山机场旅客吞吐能力",
            "全市运输机场旅客吞吐能力"};

    private static final String[] HYTTNL_PARAMS = new String[]{"江北机场货邮吞吐能力",
            "黔江机场货邮吞吐能力",
            "万州机场货邮吞吐能力",
            "巫山机场货邮吞吐能力",
            "仙女山机场货邮吞吐能力",
            "全市运输机场货邮吞吐能力"};

    private static final String[] LEVEL_PARAMS = new String[]{"江北机场等级",
            "黔江机场等级",
            "万州机场等级",
            "巫山机场等级",
            "仙女山机场等级",
            ""};

    private static final String[] HX_PARAMS = new String[]{"江北机场在飞国内航线条数",
            "黔江机场在飞国内航线条数",
            "万州机场在飞国内航线条数",
            "巫山机场在飞国内航线条数",
            "仙女山机场在飞国内航线条数",
            "江北机场在飞国际（地区）航线条数"};
    private static final String[] ALL_PARAMS = ArrayUtil.addAll(JC_PARAMS, PD_PARAMS, HYTTNL_PARAMS, LKTTNL_PARAMS, LEVEL_PARAMS, HX_PARAMS);

    @Resource
    private IndustryIndexService industryIndexService;

    public AirportInformationVo information() {

        String[] params = ArrayUtil.addAll(ALL_PARAMS, new String[]{"江北机场事件万架次率", "江北机场旅客满意度",
                "民航当年在建项目计划完成投资", "民航当年在建项目累计完成投资", "民航当年在建项目数量","民航当年在建项目投资完成率"
        });
        Map<String, IndustryIndexVo> indexVoMap = industryIndexService.getUnRealtime(params);
        AirportInformationVo result = new AirportInformationVo();
        AirportInformationVo.Item jc =
                buildItem(indexVoMap, true, JC_PARAMS);
        AirportInformationVo.Item pd =
                buildItem(indexVoMap, true, PD_PARAMS);
        AirportInformationVo.Item lkttnl =
                buildItem(indexVoMap, true, LKTTNL_PARAMS);
        AirportInformationVo.Item hyttnl =
                buildItem(indexVoMap, true, HYTTNL_PARAMS);
        AirportInformationVo.Item level =
                buildItem(indexVoMap, false, LEVEL_PARAMS);
        AirportInformationVo.Item hx =
                buildItem(indexVoMap, false, HX_PARAMS);

        result.setJc(jc.getIndexVos());
        result.setPd(pd.getIndexVos());
        result.setLkttnl(lkttnl.getIndexVos());
        result.setHyttnl(hyttnl.getIndexVos());
        result.setLevel(level.getIndexVos());
        List<IndustryIndexVo> indexVos = hx.getIndexVos();
        BigDecimal bigDecimal = IndustryIndexVo.addAll(indexVos.toArray(new IndustryIndexVo[]{}));
        indexVos.add(new IndustryIndexVo("全市运输机场总航线数量", bigDecimal.toString(), indexVos.get(0).getUnit()));
        result.setHxls(indexVos);

        industryIndexService.assembly(result, indexVoMap, CollUtil.toList(
                fun("民航当年在建项目投资完成率", AirportInformationVo::getMhjstzRate),
                fun("江北机场旅客满意度", AirportInformationVo::getLkmydRate),
                fun("江北机场事件万架次率", AirportInformationVo::getSjwjRate),
                fun("民航当年在建项目计划完成投资", AirportInformationVo::getMhjsjhtz),
                fun("民航当年在建项目累计完成投资", AirportInformationVo::getMhjswctz),
                fun("民航当年在建项目数量", AirportInformationVo::getMhjszjmns)
        ));
        return result;
    }

    private AirportInformationVo.Item buildItem(Map<String, IndustryIndexVo> indexVoMap, boolean jsTotal, String... indicatorNames) {
        AirportInformationVo.Item item = new AirportInformationVo.Item();
        industryIndexService.assembly(item, indexVoMap, CollUtil.toList(
                fun(indicatorNames[0], AirportInformationVo.Item::getJb),
                fun(indicatorNames[1], AirportInformationVo.Item::getWz),
                fun(indicatorNames[2], AirportInformationVo.Item::getWs),
                fun(indicatorNames[3], AirportInformationVo.Item::getXns),
                fun(indicatorNames[4], AirportInformationVo.Item::getQj),
                fun(indicatorNames[5], AirportInformationVo.Item::getQs)));
        if (jsTotal) {
            IndustryIndexVo itemQs = item.getQs();
            if (itemQs == null) {
                itemQs = new IndustryIndexVo(indicatorNames[5], "", item.getJb().getUnit());
            }
            BigDecimal decimal = IndustryIndexVo.addAll(item.getJb(), item.getWz(), item.getWs(), item.getXns(), item.getQj());
            itemQs.setIndicatorValue(decimal.toString());
            item.setQs(itemQs);
        }
        return item;
    }
}
